<?php
/**
 * @name CartController
 * @author xuebingwang
 * @desc 购物车控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
*/
class CartController extends MallController {
    
    /**
     * 购物车首页
     */
    public function listAction(){

        $model = new CartModel();
        $this->getView()->assign('list',$model->getCart());
        $this->getView()->assign('total',$model->getTotalPrice());
    }
    
    /**
     * 添加至购物车
     */
    public function addAction(){

        $id = intval($this->getRequest()->getQuery('id'));

        if($id == 16 && is_login()){
            $curl = new Curl();
            $user = session('user_auth');
            $resp = $curl->setData(['distributionUserId'=>$user['unionid']])
                ->setApiUrl($this->config->url->api->sdp)
                ->send('distribution/isShopkeeper');
            if(!empty($resp) && $resp['shopkeeper']){
                $this->error('您已经是店铺大掌柜了,无需再次购买!');
            }
        }

        $curl = new Curl();
        $resp = $curl->setData(array('id'=>$id))->send('mall/product/getDetail');

        if(empty($resp) || $resp['errcode'] != 0){
            $this->error('产品不存在!');
        }else{
            $product = $resp['info'];
        }

        if(intval($product['stock']) < 1){
            $this->error('产品库存不足！');
        }
        unset($product['stock']);
        unset($product['market_price']);
        $product['pic_url'] = $product['pic'][0];
        unset($product['pic']);

        $model = new CartModel();

        $model->clear();
        $f = $model->addToCart($product);

        if(IS_AJAX){
            
            $this->success('添加成功');
        }else{
            //跳至购物车列表页面
//            $this->redirect('/cart/list');
            //直接到结算页面
            $this->redirect('/cart/settle/sb.html');
        }
    }
    
    /**
     * 设置商品数量
     */
    public function setAction(){
        
        $id = intval($this->getRequest()->getQuery('goods_id'));
        $num = intval($this->getRequest()->getQuery('quantity'));
        
        $model = new CartModel();
        if($model->modNum($id,$num)){
            $data = array('msg'=>'修改成功！','status'=>1,'amount'=>price_format($model->getTotalPrice()));
            $this->ajaxReturn($data);
        }else{
            $this->error('修改失败，请重新再试！');
        }
    }
    
    /**
     * 删除指定商品
     */
    public function dropAction(){

        $id = intval($this->getRequest()->getQuery('goods_id'));
        $model = new CartModel();
        
        if($model->delItem($id)){
            $data = array('status'=>1,'amount'=>price_format($model->getTotalPrice()));
            $this->ajaxReturn($data);
        }else{
            $this->error('删除失败，请重新再试！');
        }
    }


    /**
     * 结算
     */
    public function settleAction(){
        $user = session('user_auth');
        if(empty($user) || !isset($user['unionid'])){
            $this->redirect('/login.html');
        }

//        $curl = new Curl();
//        $address = $curl->setData(['unionid'=>$user['unionid']])->send('mall/user/getAddress');
//        $address = array_merge(['id'=>'','consignee'=>'' ,'mobile'=>'','idnum'=>''],$address);
//        $this->getView()->assign('address',$address);

        $cart_model = new CartModel();

        $plist = $cart_model->getCart();

        if(empty($plist)){
            $this->redirect('/');
        }

        $this->getView()->assign('plist',$plist);
        $this->getView()->assign('amount',$cart_model->getTotalPrice());
        if(intval($user['subscribe']) < 1){
            $this->layout->alert_msg = '<p>付款完成后，请选择关注选项</p><p>您将获得店铺大掌柜资格</p><p>可以获得更多销售佣金！</p>';
        }
    }
}
